package com.servlet.news;

import com.dao.NewsDao;
import com.dao.NewsInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author admin
 */
@WebServlet(name = "NewsAddServlet",urlPatterns = "/NewsAddServlet.filter")
public class NewsAddServlet extends HttpServlet {
//    private String encoding;


    private NewsDao newsdao = new NewsInfo();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String n_title = request.getParameter("n_title");
        String u_name = request.getParameter("u_name");
        String t_name = request.getParameter("t_name");
        String n_content = request.getParameter("n_content");
        System.out.println(n_title+u_name+t_name+n_content);
        try {
            boolean b = newsdao.insertNews(n_title, u_name, t_name, n_content);

            if(b){
                System.out.println("新闻添加成功");
                response.sendRedirect(request.getContextPath()+"/NewsSelectAllServlet.filter");
            }else{
                request.getRequestDispatcher("/WEB-INF/page/find.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
